home *** CD-ROM | disk | FTP | other *** search
/ Amiga Aktuell / Amiga Aktuell.iso / libs / class.doc < prev    next >
Text File  |  1996-09-05  |  3KB  |  111 lines

  1. New features of classes
  2. =======================
  3.  
  4. A class can implement certain operations that are invoked by special
  5. syntax (such as subscription or arithmetic operations) by defining
  6. methods with special names.
  7.  
  8.  
  9. Special methods for any type
  10. ----------------------------
  11.  
  12. __repr__(self) --> string
  13.  
  14. Used by the print statement and conversions (reverse quotes) to
  15. compute the string representation of an object.
  16.  
  17. __cmp__(self, other) --> int
  18.  
  19. Used by all comparison operations.  Should return -1 if self<other, 0
  20. if self==other, +1 if self>other.  Due to limitations in the
  21. interpreter, exceptions raised by comparisons are ignored, and the
  22. objects will be considered equal in this case.
  23.  
  24.  
  25. Special methods for sequence and mapping types
  26. ----------------------------------------------
  27.  
  28. __len__(self) --> int
  29.  
  30. Used by the built-in function len().  Should return the length of the
  31. object, which should be >= 0.  Also, an object whose __len__() method
  32. returns 0 
  33.  
  34. __getitem__(self, key) --> value
  35.  
  36. Used to implement value = self[key].  Note that the special
  37. interpretation of negative keys (if the class wishes to emulate a
  38. sequence type) is up to the __getitem__ method.
  39.  
  40. __setitem__(self, key, value)
  41.  
  42. Used to implement self[key] = value.  Same note as for __getitem__.
  43.  
  44. __delitem__(self, key)
  45.  
  46. Used to implement del self[key].  Same note as for __getitem__.
  47.  
  48.  
  49. Special methods for sequence types
  50. ----------------------------------
  51.  
  52. __getslice__(self, i, j) --> sequence
  53.  
  54. Used to implement self[i:j].  Note that missing i or j are replaced by
  55. 0 or len(self), respectively, and len(self) has been added to negative
  56. i or j.
  57.  
  58. __setslice__(self, i, j, sequence)
  59.  
  60. Used to implement self[i:j] = value.  Same note as for __getslice__.
  61.  
  62. __delslice__(self, i, j)
  63.  
  64. Used to implement del self[i:j].  Same note as for __getslice__.
  65.  
  66.  
  67. Special methods for numeric types
  68. ---------------------------------
  69.  
  70. __add__, __sub__, __mul__, __div__, __mod__, __divmod__, __pow__,
  71. __lshift__, __rshift__, __and__, __xor__, __or__
  72.  
  73. Used to implement the binary arithmetic operations (divmod and pow are
  74. called by built-in functions).  All have the call pattern
  75. func(self, other) --> number.
  76.  
  77. __neg__, __pos__, __abs__, __invert__
  78.  
  79. Used to implement the unary arithmetic operations (-, +, abs and ~).
  80. All have the call pattern func(self) --> number.
  81.  
  82. __nonzero__(self) --> int
  83.  
  84. Used to implement boolean testing.  An alternative name for this
  85. method is __len__.
  86.  
  87. __coerce__(self, other) --> (self1, other1) or None
  88.  
  89. Used to implement "mixed-mode" numeric arithmetic.  Either return a
  90. tuple containing self and other converted to some common type, or None
  91. if no way of conversion is known.  When the common type would be the
  92. type of other, it is sufficient to return None, since the interpreter
  93. will also ask the other object to attempt a coercion (but sometimes,
  94. if the implementation of the other type cannot be changed, it is
  95. useful to do the conversion to the other type here).
  96.  
  97. __int__(self) --> int
  98. __long__(self) --> long
  99. __float__(self) --> float
  100.  
  101. Used to implement the built-in functions int(), long() and float().
  102.  
  103.  
  104. Notes
  105. -----
  106.  
  107. Except for __repr__ and __cmp__, when no appropriate method is
  108. defined, attempts to execute the operation raise an exception.  For
  109. __repr__ and __cmp__, the traditional interpretations are used
  110. in this case.
  111.